<?php

namespace App\Admin\Controllers;

use App\Models\GzdEngineering;
use App\Models\GzdMuban;
use App\Models\GzdWorkUser;
use App\Admin\Renderable\GzdWorkUserDetails;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Http\Controllers\AdminController;

class GzdMubanController extends AdminController
{
    protected $title = '模板库';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GzdMuban(), function (Grid $grid) {

            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->like('name', '名称')->width(5);

            });

            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('total_amount');
            $grid->column('engineering');


            $grid->column('morning_min','上午最小基础分数');
            $grid->column('morning_max','上午最大基础分数');

            $grid->column('afternoon_min','下午最小基础分数');
            $grid->column('afternoon_max','下午最大基础分数');

            $grid->column('overtime_min','加班最小基础分数');
            $grid->column('overtime_max','加班最大基础分数');

//            $grid->column('work_user_id');
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GzdMuban(), function (Show $show) {
            $show->field('id');
            $show->field('name');
//            $show->field('total_amount');
//            $show->field('engineering');
//            $show->field('work_user_id');
//            $show->field('status');
//            $show->field('created_at');
//            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GzdMuban(), function (Form $form) {
            $form->hidden('id');
            $form->text('name');
            $form->decimal('total_amount');
            $form->decimal('morning_min','上午最小基础分数');
            $form->decimal('morning_max','上午最大基础分数');

            $form->decimal('afternoon_min','下午最小基础分数');
            $form->decimal('afternoon_max','下午最大基础分数');

            $form->decimal('overtime_min','加班最小基础分数');
            $form->decimal('overtime_max','加班最大基础分数');

            $form->select('engineering','所属工程')->options(function ($engineering) {
                return GzdEngineering::where('engineering_name','<>','')->pluck('engineering_name', 'engineering_name');
            });

            $form->multipleSelectTable('work_user_id','包含人员')
                ->title('弹窗标题')
                ->dialogWidth('70%') // 弹窗宽度，默认 800px
                ->from(GzdWorkUserDetails::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                ->model(GzdWorkUser::class, 'id', 'name')->saving(function ($value) {return json_encode($value);});


            $form->saved(function (Form $form, $result) {
                // 判断是否是新增操作
                if ($form->isCreating()) {

                    $newId = $form->getKey();

                    if (!$newId) {
                        return $form->error('数据保存失败');
                    }

                    $muban = DB::table('gzd_muban')->where('id',$newId)->first();
                    $work_user_array = explode(',',str_replace(["[", "]"], "", $muban->work_user_id));

                    foreach ($work_user_array as $key => $value){
                        $work_user_info = DB::table('gzd_work_user')->where('id',str_replace('"', '', $value))->first();
                        $kaoqinbiao_muban_id = [];
                        for ($i = 1;$i<=3;$i++){
                            $kaoqin = [];
                            $kaoqin['name'] = $work_user_info->name;
                            $sum = 0;
                            if($i==1){
                                $time_slot = '上午';
                                for ($a=1;$a<=31;$a++){
                                    $fenshu = mt_rand($muban->morning_min,$muban->morning_max);
                                    $sum += $fenshu;
                                    $kaoqin['k'.$a] = $fenshu;
                                }
                            }
                            if($i==2){
                                $time_slot = '下午';
                                for ($a=1;$a<=31;$a++){
                                    $fenshu = mt_rand($muban->afternoon_min,$muban->afternoon_max);
                                    $sum += $fenshu;
                                    $kaoqin['k'.$a] = $fenshu;
                                }
                            }
                            if($i==3){
                                $time_slot = '加班';
                                for ($a=1;$a<=31;$a++){
                                    $fenshu = mt_rand($muban->overtime_min,$muban->overtime_max);
                                    $sum += $fenshu;
                                    $kaoqin['k'.$a] = $fenshu;
                                }
                            }
                            $kaoqin['time_slot'] = $time_slot;
                            $kaoqin['sum'] = $sum;
                            $kaoqin['remarks'] = '';
                            $kaoqin['muban_id'] = $newId;
                            $kaoqinbiao_muban_id[] = DB::table('gzd_kaoqinbiao_muban_details')->insertGetId($kaoqin);
                        }

                        $work_user_info = DB::table('gzd_work_user')->where('id',str_replace('"', '', $value))->first();
                        $basic_salary = DB::table('type_work')->where('name',$work_user_info->type_work)->value('basic_salary');
                        $quantity = DB::table('gzd_kaoqinbiao_muban_details')->whereIn('id',$kaoqinbiao_muban_id)->sum('sum') / 10;
                        $gongzidan = [
                            'name'=>$work_user_info->name,
                            'quantity'=>$quantity,
                            'type_work'=>$work_user_info->type_work,
                            'basic_salary'=>$basic_salary,
                            'payable'=>$basic_salary * $quantity,
                            'actual'=>0,
                            'other'=>0,
                            'advance'=>$basic_salary * $quantity,
                            'bank_number'=>$work_user_info->bank_number,
                            'bank_deposit'=>$work_user_info->bank_deposit,
                            'muban_id'=>$newId,
                            'kaoqinbiao_muban_id'=>implode(',',$kaoqinbiao_muban_id)
                        ];
                        $gzd_muban_details_id = DB::table('gzd_muban_details')->insertGetId($gongzidan);
                        DB::table('gzd_kaoqinbiao_muban_details')->whereIn('id',$kaoqinbiao_muban_id)->update(['gzd_muban_details_id'=>$gzd_muban_details_id]);
                    }

                }else{
                    // 修改操作
                    DB::table('gzd_kaoqinbiao_muban_details')->where('muban_id',$form->id)->delete();
                    DB::table('gzd_muban_details')->where('muban_id',$form->id)->delete();


                    $muban = DB::table('gzd_muban')->where('id',$form->id)->first();
                    $work_user_array = explode(',',str_replace(["[", "]"], "", $muban->work_user_id));

                    foreach ($work_user_array as $key => $value){
                        $work_user_info = DB::table('gzd_work_user')->where('id',str_replace('"', '', $value))->first();
                        $kaoqinbiao_muban_id = [];
                        for ($i = 1;$i<=3;$i++){
                            $kaoqin = [];
                            $kaoqin['name'] = $work_user_info->name;
                            $sum = 0;
                            if($i==1){
                                $time_slot = '上午';
                                for ($a=1;$a<=31;$a++){
                                    $fenshu = mt_rand($muban->morning_min,$muban->morning_max);
                                    $sum += $fenshu;
                                    $kaoqin['k'.$a] = $fenshu;
                                }
                            }
                            if($i==2){
                                $time_slot = '下午';
                                for ($a=1;$a<=31;$a++){
                                    $fenshu = mt_rand($muban->afternoon_min,$muban->afternoon_max);
                                    $sum += $fenshu;
                                    $kaoqin['k'.$a] = $fenshu;
                                }
                            }
                            if($i==3){
                                $time_slot = '加班';
                                for ($a=1;$a<=31;$a++){
                                    $fenshu = mt_rand($muban->overtime_min,$muban->overtime_max);
                                    $sum += $fenshu;
                                    $kaoqin['k'.$a] = $fenshu;
                                }
                            }
                            $kaoqin['time_slot'] = $time_slot;
                            $kaoqin['sum'] = $sum;
                            $kaoqin['remarks'] = '';
                            $kaoqin['muban_id'] = $form->id;
                            $kaoqinbiao_muban_id[] = DB::table('gzd_kaoqinbiao_muban_details')->insertGetId($kaoqin);
                        }

                        $work_user_info = DB::table('gzd_work_user')->where('id',str_replace('"', '', $value))->first();
                        $basic_salary = DB::table('type_work')->where('name',$work_user_info->type_work)->value('basic_salary');
                        $quantity = DB::table('gzd_kaoqinbiao_muban_details')->whereIn('id',$kaoqinbiao_muban_id)->sum('sum') / 10;
                        $gongzidan = [
                            'name'=>$work_user_info->name,
                            'quantity'=>$quantity,
                            'type_work'=>$work_user_info->type_work,
                            'basic_salary'=>$basic_salary,
                            'payable'=>$basic_salary * $quantity,
                            'actual'=>0,
                            'other'=>0,
                            'advance'=>$basic_salary * $quantity,
                            'bank_number'=>$work_user_info->bank_number,
                            'bank_deposit'=>$work_user_info->bank_deposit,
                            'muban_id'=>$form->id,
                            'kaoqinbiao_muban_id'=>implode(',',$kaoqinbiao_muban_id)
                        ];
                        $gzd_muban_details_id = DB::table('gzd_muban_details')->insertGetId($gongzidan);
                        DB::table('gzd_kaoqinbiao_muban_details')->whereIn('id',$kaoqinbiao_muban_id)->update(['gzd_muban_details_id'=>$gzd_muban_details_id]);
                    }
                }


            });


        });







    }


//    public function gzd_muban_user(Request $request)
//    {
//        $engineering_id = $request->get('q');
//        $engineering = DB::table('gzd_engineering')->where('id',$engineering_id)->value('engineering_name');
//        return GzdWorkUser::where('deleted_at',null)->where('engineering',$engineering)->pluck('name', 'name');
//    }

}
